package com.gper.edu.promise

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Promise
import scala.util._
case class TaxCut(reduction:Int)

object FirstPromiseDemo {

  def redeemCampaignPledge(): Future[TaxCut] = {
    val p = Promise[TaxCut]()
    Future {
      println("Starting the new legislative period.")
      Thread.sleep(2000)
      p.success(TaxCut(20))
      println("We reduced the taxes! You must reelect us!!!!1111")
    }
    p.future
  }


  def main(args: Array[String]): Unit = {
    val result = redeemCampaignPledge()
    result.onComplete{
      case Success(reduction) => println(s"A miracle! They really cut our taxes by $reduction percentage points!")
      case Failure(ex) => println(s"They broke their promises! Again! Because of a ${ex.getMessage}")
    }
    Thread.sleep(5000)
  }
}
